package com.a8hcoffee.zxingdemo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.WriterException;


import zxing.android.CaptureActivity;
import zxing.encode.CodeCreator;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_SCANNER = 10010;
    private TextView tv_result,tv_qr;
    private EditText et_content;
    private ImageView iv_qrImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        tv_result = (TextView) findViewById(R.id.tv_result);
        et_content = (EditText) findViewById(R.id.et_content);
        iv_qrImage = (ImageView) findViewById(R.id.iv_qrImage);
        tv_qr = (TextView) findViewById(R.id.tv_qr);
        et_content.requestFocus();
    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_scan:
                Intent intent = new Intent(this, CaptureActivity.class);
                startActivityForResult(intent,REQUEST_CODE_SCANNER);
                break;
            case R.id.btn_create:
                String content = et_content.getText().toString();
                if (!TextUtils.isEmpty(content)) {
                    //根据字符串生成二维码图片并显示在界面上，第二个参数为图片的大小（350*350）
                    Bitmap qrCodeBitmap = null;
                    try {
                        qrCodeBitmap = CodeCreator.createQRCode(content, 600);
                    } catch (WriterException e) {
                        e.printStackTrace();
                    }
                    if (qrCodeBitmap != null)
                    iv_qrImage.setImageBitmap(qrCodeBitmap);
                    tv_qr.setText(content);
                }else {
                    Toast.makeText(this, "输入不为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_SCANNER && resultCode == RESULT_OK){
            if (data != null){
                String result = data.getStringExtra("codedContent");
                if (!TextUtils.isEmpty(result)){
                    if (result.startsWith("http://") || result.startsWith("https://")){
                        Intent it = new Intent(this, WebActivity.class);
                        it.putExtra("path",result);
                        startActivity(it);
                    }
                }

                tv_result.setText(result);
            }
        }
    }

    /**
     * 利用正则表达式判断字符串是否是纯数字
     * @param str
     * @return
     */
//    private boolean isNumeric(String str){
//        Pattern pattern = Pattern.compile("[0-9]*");
//        Matcher isNum = pattern.matcher(str);
//        if( !isNum.matches() ){
//            return false;
//        }
//        return true;
//    }
}
